package bTree.LeetCode;

import java.util.LinkedList;
import java.util.Queue;

public class 二叉树的最小深度 {
    int ans = 1;

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        bfs(root);
        return ans;
    }

    private void bfs(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                if (queue.peek().left == null && queue.peek().right == null) {
                    return;
                }
                if (queue.peek().left != null) {
                    queue.add(queue.peek().left);
                }
                if (queue.peek().right != null) {
                    queue.add(queue.peek().right);
                }
                queue.poll();

            }
            ans++;

        }

    }
}